package com.guqj.basis.datastruct;

public class Stack<T> {

	private Object[] items;
	private final static int DEFAULT_CAPACITY = 10;
	private int top = -1;

	public Stack() throws Exception {
		this(DEFAULT_CAPACITY);
	}

	public Stack(int capacity) throws Exception {
		if (capacity <= 0) {
			throw new Exception();
		}
		this.items = new Object[capacity];
	}

	public boolean empty() {
		return top == -1;
	}

	public void push(T e) {
		if (top < items.length - 1) {
			items[++top] = e;
		} else {
			System.out.println("overflow");
		}
	}

	@SuppressWarnings("unchecked")
	public T pop() {
		if (empty()) {
			return null;
		} else {
			T e = (T) items[top];
			top--;
			return e;
		}
	}
	
	public static void main(String[] args) throws Exception {
		Stack<String> s = new Stack<>(20);
		s.push("sss");
		s.push("aaaa");
		s.push("wwww");
		System.out.println(s.pop());
		System.out.println(s.pop());
		System.out.println(s.pop());
	}
}
